package wangxiaotao.config;


import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.errors.MinioException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import wangxiaotao.properties.MyMinioProperties;

@Configuration
public class MinioConfig {
    @Autowired
    private MyMinioProperties minioProperties;

    @Bean
    public MinioClient minioClient() {
        MinioClient minioClient = MinioClient.builder()
                .endpoint("http://"+minioProperties.getEndpoint()+":"+minioProperties.getPort())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
        try {
            boolean b = minioClient.bucketExists(BucketExistsArgs.builder().bucket(minioProperties.getBucketName()).build());
            if (!b){
                throw new MinioException("minio->桶不存在");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return minioClient;
    }

}
